package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysUserWechat;
import com.ruoyi.system.mapper.SysUserWechatMapper;
import com.ruoyi.system.service.ISysUserWechatService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 微信用户绑定Service业务层处理
 *
 * @author shuyan
 * @date 2021-06-16
 */
@Service
public class SysUserWechatServiceImpl extends ServiceImpl<SysUserWechatMapper, SysUserWechat> implements ISysUserWechatService {

    @Override
    public List<SysUserWechat> queryList(SysUserWechat sysUserWechat) {
        LambdaQueryWrapper<SysUserWechat> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sysUserWechat.getUserName())) {
            lqw.like(SysUserWechat::getUserName, sysUserWechat.getUserName());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getOpenid())) {
            lqw.eq(SysUserWechat::getOpenid, sysUserWechat.getOpenid());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getUnionid())) {
            lqw.eq(SysUserWechat::getUnionid, sysUserWechat.getUnionid());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getNickname())) {
            lqw.like(SysUserWechat::getNickname, sysUserWechat.getNickname());
        }
        if (sysUserWechat.getSex() != null) {
            lqw.eq(SysUserWechat::getSex, sysUserWechat.getSex());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getLanguage())) {
            lqw.eq(SysUserWechat::getLanguage, sysUserWechat.getLanguage());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getCity())) {
            lqw.eq(SysUserWechat::getCity, sysUserWechat.getCity());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getProvince())) {
            lqw.eq(SysUserWechat::getProvince, sysUserWechat.getProvince());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getCountry())) {
            lqw.eq(SysUserWechat::getCountry, sysUserWechat.getCountry());
        }
        if (StringUtils.isNotBlank(sysUserWechat.getHeadimgurl())) {
            lqw.eq(SysUserWechat::getHeadimgurl, sysUserWechat.getHeadimgurl());
        }
        return this.list(lqw);
    }

    @Override
    public SysUserWechat getOneByUserName(String userName) {
        return lambdaQuery().eq(SysUserWechat::getUserName, userName).one();
    }

    @Override
    public SysUserWechat getOneByOpenid(String openid) {
        return lambdaQuery().eq(SysUserWechat::getOpenid, openid).one();
    }

    @Override
    public List<SysUserWechat> listWechatUsersByPhone(String phone) {
        return getBaseMapper().listWechatUsersByPhone(phone);
    }

    @Override
    public List<SysUser> listSysUsersByOpenid(String openid) {
        return getBaseMapper().listSysUsersByOpenid(openid);
    }

}
